package com.hotelManager.www.controller;

import com.hotelManager.www.po.*;
import com.hotelManager.www.service.*;
import com.hotelManager.www.common.Result;
import com.hotelManager.www.common.ResultCode;
import java.math.BigDecimal;
import java.util.List;
import java.util.ArrayList;





public class MerchantController  {

    private final ShopService shopService;
    private final RoomService roomService;
    private final OrderService orderService;
    private final UserService userService;
    private final BlacklistService blacklistService;
    private final RoomTypeService roomTypeService;

    public MerchantController() {
        this.shopService = new ShopService();
        this.roomService = new RoomService();
        this.orderService = new OrderService();
        this.userService = new UserService();
        this.blacklistService = new BlacklistService();
        this.roomTypeService = new RoomTypeService();
    }
//店铺注册
    public Result<Shop> register(Shop shop, Integer userId) {
        try {
            boolean success = shopService.registerShop(shop, userService.findById(userId));
            if (success) {
                return Result.success(shop, "店铺注册成功，等待管理员审核");
            }
            return Result.failure(ResultCode.SYSTEM_ERROR, "店铺注册失败");
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }
//更新店铺信息
    public Result<Shop> updateShopInfo(Integer shopId, Shop shop) {
        try {
            shop.setId(shopId);
            boolean success = shopService.update(shop);
            if (success) {
                return Result.success(shop, "店铺信息更新成功");
            }
            return Result.failure(ResultCode.SYSTEM_ERROR, "店铺信息更新失败");
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }


    //增加房型和房间
    public Result<RoomType> addRoomType(RoomType roomType) {
        try {
            boolean success = roomTypeService.createRoomTypeWithRooms(roomType, roomType.getStock());
            if (success) {
                return Result.success(roomType, "房型及对应房间添加成功");
            }
            return Result.failure(ResultCode.SYSTEM_ERROR, "房型添加失败");
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }
//更新房型和房间
    public Result<RoomType> updateRoomType(Integer roomTypeId, RoomType roomType) {
        try {
            roomType.setId(roomTypeId);
            boolean success = roomTypeService.updateRoomTypeWithRooms(roomType, roomType.getStock());
            if (success) {
                return Result.success(roomType, "房型及对应房间更新成功");
            }
            return Result.failure(ResultCode.SYSTEM_ERROR, "房型更新失败");
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }
//删除房型和房间
    public Result<Void> deleteRoomType(Integer roomTypeId) {
        try {
            boolean success = roomTypeService.deleteRoomTypeWithRooms(roomTypeId);
            if (success) {
                return Result.success(null, "房型及对应房间删除成功");
            }
            return Result.failure(ResultCode.SYSTEM_ERROR, "房型删除失败");
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }
//查看房型
    public Result<List<RoomType>> getRoomTypes(Integer shopId) {
        try {
            List<RoomType> roomTypes = roomTypeService.getRoomTypesByShopId(shopId);
            return Result.success(roomTypes);
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }
    //查看该酒店的房间
    public Result<List<Room>> getRoomsByUserId(Integer userId) {
        try {
            // 根据用户ID获取店铺ID
            Shop shop = shopService.getShopByUserId(userId);
            if (shop == null) {
                return Result.failure(ResultCode.NOT_FOUND, "未找到对应的店铺");
            }

            // 根据店铺ID获取房型ID列表
            List<RoomType> roomTypes = roomTypeService.getRoomTypesByShopId(shop.getId());
            if (roomTypes.isEmpty()) {
                return Result.failure(ResultCode.NOT_FOUND, "未找到对应的房型");
            }

            // 根据房型ID列表获取房间数据
            List<Room> rooms = new ArrayList<>();
            for (RoomType roomType : roomTypes) {
                rooms.addAll(roomService.getRoomsByRoomTypeId(roomType.getId()));
            }

            return Result.success(rooms);
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }

// 根据用户ID获取店铺信息
    public Result<Shop> getShopByUserId(Integer userId) {
        try {
            Shop shop = shopService.getShopByUserId(userId);
            if (shop != null) {
                return Result.success(shop);
            }
            return Result.failure(ResultCode.NOT_FOUND, "未找到对应的店铺");
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }

//核销订单
    public Result<Order> verifyOrder(Integer orderId, Integer merchantId) {
        try {
            boolean success = orderService.completeOrder(orderId, merchantId);
            if (success) {
                return Result.success(null, "订单核销成功");
            }
            return Result.failure(ResultCode.SYSTEM_ERROR, "订单核销失败");
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }
//通过退房申请
    public Result<Order> processRefund(Integer orderId, Integer shopId, boolean approved) {
        try {
            boolean success = orderService.processRefundRequest(orderId, shopId, approved);
            if (success) {
                return Result.success(null, approved ? "退订申请已通过" : "退订申请已拒绝");
            }
            return Result.failure(ResultCode.SYSTEM_ERROR, "处理退订申请失败");
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }
//查看店铺销量
    public Result<Integer> getSalesVolume(Integer shopId) {
        try {
            Shop shop = shopService.findById(shopId);
            if (shop != null) {
                return Result.success(shop.getSalesVolume());
            }
            return Result.failure(ResultCode.SYSTEM_ERROR, "获取销量失败");
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }

//查看营业额
    public Result<BigDecimal> getRevenue(Integer shopId) {
        try {
            Shop shop = shopService.findById(shopId);
            if (shop != null) {
                return Result.success(shop.getRevenue());
            }
            return Result.failure(ResultCode.SYSTEM_ERROR, "获取营业额失败");
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }

//拉黑用户
    public Result<Void> addUserToBlacklist(Integer shopId, Integer userId) {
        try {
            boolean success = blacklistService.addUserToBlacklist(shopId, userId);
            if (success) {
                return Result.success(null, "用户已被加入黑名单");
            }
            return Result.failure(ResultCode.SYSTEM_ERROR, "加入黑名单失败");
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }

    public Result<List<UserBlacklist>> getBlacklistByShopId(Integer shopId) {
        try {
            List<UserBlacklist> blacklist = blacklistService.getBlacklistByShopId(shopId);
            return Result.success(blacklist);
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }
//移除用户
    public Result<Void> removeUserFromBlacklist(Integer blacklistId) {
        try {
            boolean success = blacklistService.removeUserFromBlacklist(blacklistId);
            if (success) {
                return Result.success(null, "用户已从黑名单中移除");
            }
            return Result.failure(ResultCode.SYSTEM_ERROR, "从黑名单移除失败");
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }

// 获取待处理订单
    public Result<List<Order>> getPendingOrders(Integer shopId) {
        try {
            List<Order> pendingOrders = orderService.getOrdersByShopId(shopId);
            return Result.success(pendingOrders);
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }

    // 获取已完成订单
    public Result<List<Order>> getCompletedOrders(Integer shopId) {
        try {
            List<Order> completedOrders = orderService.getOrdersByShopId(shopId);
            return Result.success(completedOrders);
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }

    // 获取退订申请订单
    public Result<List<Order>> getRefundingOrders(Integer shopId) {
        try {
            List<Order> refundRequests = orderService.getRefundingOrdersByShopId(shopId);
            return Result.success(refundRequests);
        } catch (Exception e) {
            return Result.failure(ResultCode.SYSTEM_ERROR, e.getMessage());
        }
    }


}